home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / BF_SDK11.ZIP / PAS_DEMO.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-06-10  |  10.8 KB  |  316 lines

  1.  
  2. {
  3.         PAS_DEMO.PAS
  4.         test program for compatibility & speed of BLOWFISH.TPU
  5.         language   : Turbo/Borland Pascal
  6.         last update: 06/06/96
  7.         (c)1996 Markus Hahn & Cedric Reinartz
  8. }
  9.  
  10.  
  11. program PAS_DEMO;
  12. uses Crt, Dos, Blowfish;
  13.  
  14.  
  15. const
  16.   { official test vectors from DDJ 10/95... }
  17.   key1        : String = 'abcdefghijklmnopqrstuvwxyz';
  18.   testdata1_p : array[1..2] of ULONG = ($424c4f57, $46495348);
  19.   testdata1_c : array[1..2] of ULONG = ($324ed0fe, $f413a203);
  20.  
  21.   key2        : String = 'Who is John Galt?';
  22.   testdata2_p : array[1..2] of ULONG = ($fedcba98, $76543210);
  23.   testdata2_c : array[1..2] of ULONG = ($cc91732b, $8022f684);
  24.  
  25.   BIGBUFSIZE  = 32000;
  26.   TESTLOOPS   = 128;    { suitable for a DX4 }
  27.   SCRLOOPS    = 1024;
  28.  
  29.  
  30.  
  31. {
  32.         ULONG2HEX - converts a ULONG into a string
  33.         -> ULONG
  34.         <- String
  35. }
  36. function ULONG2Hex(ulTheVal : ULONG) : String;
  37. const
  38.   HEXTAB : String[16] = '0123456789abcdef';
  39. var
  40.       nI : Integer;
  41.    sTemp : String[8];
  42. begin
  43.   sTemp[0]:=Chr(8);
  44.   for nI:=0 to 7 do
  45.     sTemp[8-nI]:=HEXTAB[((ulTheVal shr (nI*4)) and $0f) + 1];
  46.   ULONG2Hex:=sTemp;
  47. end;
  48.  
  49.  
  50. {
  51.         RANDOM32 - creates a 32 bit random value
  52.         <- random ULONG
  53. }
  54. function Random32 : ULONG;
  55. begin
  56.   Random32:=(ULONG(Random($ffff)) shl 16) or Random($ffff);
  57. end;
  58.  
  59.  
  60. { big buffer for benchmark }
  61. type
  62.   PBigBuf = ^TBigBuf;
  63.   TBigBuf = array[1..BIGBUFSIZE] of Byte;
  64.  
  65.  
  66. { global data... }
  67. var
  68.   ulCBCIVL   : ULONG;   { IV = initialisation vector }
  69.   ulCBCIVR   : ULONG;
  70.   ulCBCLeft  : ULONG;
  71.   ulCBCRight : ULONG;
  72.   ulEqual    : ULONG;
  73.   sMyKey     : String;
  74.   testbuf    : array[1..6] of ULONG;
  75.   unI        : UINT;
  76.   biggy      : PBigBuf;
  77.   ulStart    : ULONG;
  78.   ulTime     : ULONG;
  79.   ulRate     : ULONG;
  80.   h,m,s,c    : UINT;
  81.   pBoxes     : P_ULONG_Buffer;
  82.   BF_key     : array[1..1058] of ULONG;
  83.   BF_keysave : array[1..1058] of ULONG;
  84.   pScreen    : P_ULONG_Buffer;
  85.  
  86.  
  87.  
  88. begin
  89.      ClrScr;
  90.      Randomize;
  91.  
  92.      Blowfish_GetBoxes(@BF_key[1]);      { save original boxes }
  93.      { the following compare is not necessary. Just to show the function }
  94.      if Blowfish_SetRounds(16) <> 16 then writeln('Could not set Rounds');
  95.  
  96.      { Are we compatible? }
  97.  
  98.      { test vector #1 }
  99.      TextColor(10); WriteLn('test data #1...'); TextColor(7);
  100.  
  101.      Blowfish_Init(@key1[1], Length(key1));
  102.      { show the first 6 p-boxes }
  103.      pBoxes:=Blowfish_GetBoxPointer;
  104.      Write('pboxes : ');
  105.      for unI:=0 to 5 do
  106.        Write(ULONG2Hex(pBoxes^[unI]), ' ');
  107.      WriteLn;
  108.      { now en-/decrypt and compare with the official values }
  109.      WriteLn('plaintext : ', ULONG2Hex(testdata1_p[1]),
  110.              ' ',            ULONG2Hex(testdata1_p[2]));
  111.      Blowfish_ECBEncrypt(@testdata1_p[1], 8);
  112.      WriteLn('ciphertext: ', ULONG2Hex(testdata1_p[1]),
  113.              ' ',            ULONG2Hex(testdata1_p[2]));
  114.      WriteLn('DDJ 10/95 : ', ULONG2Hex(testdata1_c[1]),
  115.              ' ',            ULONG2Hex(testdata1_c[2]));
  116.      Blowfish_ECBDecrypt(@testdata1_p[1], 8);
  117.      WriteLn('decrypted : ', ULONG2Hex(testdata1_p[1]),
  118.              ' ',            ULONG2Hex(testdata1_p[2]));
  119.  
  120.      { #2 }
  121.      TextColor(10); WriteLn('test data #2...'); TextColor(7);
  122.      Blowfish_SetBoxes(@BF_key[1]);      { reload boxes }
  123.      Blowfish_Init(@key2[1], Length(key2));
  124.      pBoxes:=Blowfish_GetBoxPointer;
  125.      Write('pboxes : ');
  126.      for unI:=0 to 5 do
  127.        Write(ULONG2Hex(pBoxes^[unI]), ' ');
  128.      WriteLn;
  129.      WriteLn('plaintext : ', ULONG2Hex(testdata2_p[1]),
  130.              ' ',            ULONG2Hex(testdata2_p[2]));
  131.      Blowfish_ECBEncrypt(@testdata2_p[1], 8);
  132.      WriteLn('ciphertext: ', ULONG2Hex(testdata2_p[1]),
  133.              ' ',            ULONG2Hex(testdata2_p[2]));
  134.      WriteLn('DDJ 10/95 : ', ULONG2Hex(testdata2_c[1]),
  135.              ' ',            ULONG2Hex(testdata2_c[2]));
  136.      Blowfish_ECBDecrypt(@testdata2_p[1], 8);
  137.      WriteLn('decrypted : ', ULONG2Hex(testdata2_p[1]),
  138.              ' ',            ULONG2Hex(testdata2_p[2]));
  139.      WriteLn(#13#10, 'press a key ...', #13#10);
  140.      ReadKey;
  141.  
  142.      { test ECB routines, work with small buffer... }
  143.      { 16 rounds }
  144.      Blowfish_SetBoxes(@BF_key[1]);      { reload boxes }
  145.      sMyKey:='Who wants some?';
  146.      Blowfish_Init(@sMyKey[1], Length(sMyKey));
  147.      TextColor(10); WriteLn('ECB test, 16 rounds...'); TextColor(7);
  148.      WriteLn('original data:');
  149.      { generate and show random numbers...}
  150.      for unI:=1 to 6 do begin
  151.        testbuf[unI]:=Random32;
  152.        Write(ULONG2Hex(testbuf[unI]), '  ');
  153.      end;
  154.      WriteLn;
  155.      WriteLn('encrypted data:');
  156.      Blowfish_ECBEncrypt(@testbuf[1], 6*4);
  157.      for unI:=1 to 6 do Write(ULONG2Hex(testbuf[unI]), '  ');
  158.      WriteLn;
  159.      WriteLn('decrypted data:');
  160.      Blowfish_ECBDecrypt(@testbuf[1], 6*4);
  161.      for unI:=1 to 6 do Write(ULONG2Hex(testbuf[unI]), '  ');
  162.      WriteLn;
  163.      { 32 rounds }
  164.      Blowfish_SetRounds(32);      { set number of encryption rounds to 32 }
  165.      Blowfish_SetBoxes(@BF_key[1]);      { reload boxes }
  166.      sMyKey:='Who wants some?';
  167.      Blowfish_Init(@sMyKey[1], Length(sMyKey));
  168.      TextColor(10); WriteLn('ECB test, 32 rounds...'); TextColor(7);
  169.      WriteLn('original data:');
  170.      for unI:=1 to 6 do Write(ULONG2Hex(testbuf[unI]), '  ');
  171.      WriteLn;
  172.      WriteLn('encrypted data:');
  173.      Blowfish_ECBEncrypt(@testbuf[1], 6*4);
  174.      for unI:=1 to 6 do Write(ULONG2Hex(testbuf[unI]), '  ');
  175.      WriteLn;
  176.      WriteLn('decrypted data:');
  177.      Blowfish_ECBDecrypt(@testbuf[1], 6*4);
  178.      for unI:=1 to 6 do Write(ULONG2Hex(testbuf[unI]), '  ');
  179.      WriteLn; WriteLn;
  180.  
  181.      WriteLn(#13#10, 'press a key...', #13#10);
  182.      ReadKey;
  183.  
  184.      { now test the CBC routines... }
  185.      { 16 rounds }
  186.      Blowfish_SetRounds(16);
  187.      sMyKey:='Damn, I''m looking good!';
  188.      TextColor(10); WriteLn('CBC test, 16 rounds...'); TextColor(7);
  189.      ulCBCLeft:=Random32;     { create an IV "server" }
  190.      ulCBCRight:=Random32;
  191.      ulCBCIVL:=ulCBCLeft;
  192.      ulCBCIVR:=ulCBCRight;
  193.      WriteLn('IV: ', ULONG2Hex(ulCBCIVL),
  194.              ' ',    ULONG2Hex(ulCBCIVR));
  195.      WriteLn('original data:');
  196.      for unI:=1 to 6 do Write(ULONG2Hex(testbuf[unI]), '  ');
  197.      WriteLn;
  198.      WriteLn('encrypted data:');
  199.      Blowfish_SetBoxes(@BF_key[1]);    { reload boxes }
  200.      Blowfish_Init(@sMyKey[1], Length(sMyKey));
  201.      Blowfish_CBCEncrypt(@testbuf[1], 6*4, ulCBCIVL, ulCBCIVR);
  202.      for unI:=1 to 6 do Write(ULONG2Hex(testbuf[unI]), '  ');
  203.      WriteLn;
  204.      WriteLn('decrypted data :');
  205.      { just do some overhead for provocation :) }
  206.      sMyKey:='Damn, I''m looking good!Damn, I''m looking good!';
  207.      Blowfish_SetBoxes(@BF_key[1]);
  208.      Blowfish_Init(@sMyKey[1], Length(sMyKey));
  209.      { decrypt in 2 parts to double check the TPU }
  210.      ulCBCIVL:=ulCBCLeft;
  211.      ulCBCIVR:=ulCBCRight;
  212.      Blowfish_CBCDecrypt(@testbuf[1], 2*4, ulCBCIVL, ulCBCIVR);
  213.      Blowfish_CBCDecrypt(@testbuf[3], 4*4, ulCBCIVL, ulCBCIVR);
  214.      for unI:=1 to 6 do Write(ULONG2Hex(testbuf[unI]), '  ');
  215.      WriteLn;
  216.      { 32 rounds }
  217.      Blowfish_SetRounds(32);
  218.      ulCBCIVL  :=ulCBCLeft;
  219.      ulCBCIVR  :=ulCBCRight;
  220.      TextColor(10); WriteLn('CBC test, 32 rounds...'); TextColor(7);
  221.      WriteLn('IV: ', ULONG2Hex(ulCBCIVL),
  222.              ' ',    ULONG2Hex(ulCBCIVR));
  223.      WriteLn('original data :');
  224.      for unI:=1 to 6 do Write(ULONG2Hex(testbuf[unI]), '  ');
  225.      WriteLn;
  226.      WriteLn('encrypted data :');
  227.      Blowfish_SetBoxes(@BF_key[1]);      { reload Boxes }
  228.      sMyKey:='Damn, I''m looking good!';
  229.      Blowfish_Init(@sMyKey[1], Length(sMyKey));
  230.      Blowfish_CBCEncrypt(@testbuf[1], 6*4, ulCBCLeft, ulCBCRight);
  231.      for unI:=1 to 6 do Write(ULONG2Hex(testbuf[unI]), '  ');
  232.      WriteLn;
  233.      WriteLn('decrypted data :');
  234.      Blowfish_SetBoxes(@BF_key[1]);      { reload Boxes }
  235.      Blowfish_Init(@sMyKey[1], Length(sMyKey));
  236.      { decrypt in 2 parts to double check the correct reentry }
  237.      Blowfish_CBCDecrypt(@testbuf[1], 2*4, ulCBCIVL, ulCBCIVR);
  238.      Blowfish_CBCDecrypt(@testbuf[3], 4*4, ulCBCIVL, ulCBCIVR);
  239.      for unI:=1 to 6 do Write(ULONG2Hex(testbuf[unI]), '  ');
  240.      WriteLn; WriteLn;
  241.  
  242.     { benchmark, using CBC and a 32kB test buffer }
  243.     New(biggy);
  244.     TextColor(10); WriteLn('performance with 16 rounds...'); TextColor(7);
  245.     Blowfish_SetRounds(16);
  246.     GetTime(h,m,s,c);
  247.     ulStart:=ULONG(h)*3600*100 + ULONG(m)*60*100 + ULONG(s)*100 + ULONG(c);
  248.     for unI:=1 to TESTLOOPS do
  249.       Blowfish_CBCEncrypt(@biggy^[1], BIGBUFSIZE, ulCBCLeft, ulCBCRight);
  250.     GetTime(h,m,s,c);
  251.     ulTime:=(ULONG(h)*3600*100 + ULONG(m)*60*100 + ULONG(s)*100 + ULONG(c))-ulStart;
  252.     ulRate:=((TESTLOOPS*BIGBUFSIZE) div ulTime) * 100;
  253.     WriteLn(ulRate,  ' bytes per second');
  254.  
  255.     TextColor(10); WriteLn('performance with 32 rounds...'); TextColor(7);
  256.     Blowfish_SetRounds(32);
  257.     GetTime(h,m,s,c);
  258.     ulStart:=ULONG(h)*3600*100 + ULONG(m)*60*100 + ULONG(s)*100 + ULONG(c);
  259.     for unI:=1 to TESTLOOPS do
  260.       Blowfish_CBCEncrypt(@biggy^[1], BIGBUFSIZE, ulCBCLeft, ulCBCRight);
  261.     GetTime(h,m,s,c);
  262.     ulTime:=(ULONG(h)*3600*100 + ULONG(m)*60*100 + ULONG(s)*100 + ULONG(c))-ulStart;
  263.     ulRate:=((TESTLOOPS*BIGBUFSIZE) div ulTime) * 100;
  264.     WriteLn(ulRate,  ' bytes per second');
  265.  
  266.     Dispose(biggy);
  267.  
  268.  
  269.     { check the weak key detector... }
  270.     TextColor(10); WriteLn(#13#10+'weak key check...'); TextColor(7);
  271.  
  272.     Blowfish_SetRounds(16);
  273.     Blowfish_GetBoxes(@BF_key);
  274.     Blowfish_SetRounds(32);
  275.     Blowfish_GetBoxes(@BF_keysave);
  276.  
  277.     Write('16 rounds: ', Blowfish_WeakKey);
  278.     Blowfish_SetRounds(16);
  279.     ulEqual:=Random32;
  280.     BF_key[18+Random(1024)]:=ulEqual;
  281.     BF_key[18+Random(1024)]:=ulEqual;
  282.     Blowfish_SetBoxes(@BF_key);
  283.     WriteLn(' ', Blowfish_WeakKey);
  284.  
  285.     Blowfish_SetRounds(32);
  286.     Blowfish_SetBoxes(@BF_keysave);
  287.     Write('32 rounds: ', Blowfish_WeakKey);
  288.     ulEqual:=Random32;
  289.     BF_keysave[18+Random(1024)]:=ulEqual;
  290.     BF_keysave[18+Random(1024)]:=ulEqual;
  291.     Blowfish_SetBoxes(@BF_keysave);
  292.     WriteLn(' ', Blowfish_WeakKey);
  293.  
  294.     { do something funny,
  295.       encrypt the screen... }
  296.  
  297.     WriteLn(#13#10,
  298.             'We will now encrypt your video memory. If the flicker has'+#13#10,
  299.             'stopped you must press a key to decrypt/restore it again.'+#13#10,
  300.             'You''ll also saw some equal parts, a good example _not_ to'+#13#10,
  301.             'use ECB but CBC :)'+#13#10#13#10,
  302.             'press a key...');
  303.     ReadKey;
  304.     Blowfish_SetRounds(16);
  305.     { assume 80x25 color textmode }
  306.     pScreen:=Ptr($b800, 0);
  307.     { Blowfish is so fast we do some extra loops for better animation here,
  308.       not necessary in the real world, of course }
  309.     for unI:=1 to SCRLOOPS do Blowfish_ECBEncrypt(pScreen, 4000);
  310.     ReadKey;
  311.     for unI:=1 to SCRLOOPS do Blowfish_ECBDecrypt(pScreen, 4000);
  312.     WriteLn(#13#10+'That''s all folks!');
  313.  
  314.  
  315. end.
  316.